package com.zhuss.product.processor;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.zhuss.bean.bo.product.product.SearchProductBo;
import com.zhuss.bean.vo.product.product.ListProductVo;
import com.zhuss.common.ListVo;
import com.zhuss.common.PageVo;
import com.zhuss.product.db.dao.ext.ProductDao;
import com.zhuss.product.db.entity.Product;
import com.zhuss.exception.OceanException;
import com.zhuss.exception.ExceptionCode;


/**
 * @description: 商品读
 * @author: zhushanshuo
 * @create: Wed Mar 24 22:26:45 CST 2021
 **/
@Component("productRead")
public class ProductRead {
	
	@Autowired
	private ProductDao productDao;
	
	
	public Map<String, Product> listMap() {
		List<Product> list = productDao.listAll();
		Map<String, Product> map = new HashMap<>();
		for (Product c : list) {
			map.put(c.getName(), c);
		}
		return map;
	}
	
	/**
	 * 商品
	 * @return
	 */
	public Object listProduct() {
		List<Product> productList = productDao.listAll();
		List<ListVo> result = new ArrayList<>();
		transferList(productList, result);
		return result;
	}
	
	
	/**
	 * 商品
	 * @return
	 */
	public Object listProduct(List<Long> productIdList) {
		List<Product> result = productDao.listById(productIdList);
		return result;
	}
	
	
	/**
	 * 根据名字查询商品
	 * @param 
	 * @return
	 */
	public List<Product> list(String name) {
		List<Product> result = productDao.listByName(name);
		return result;
	}
	
	public List<Product> listNotId(String name, Long productId) {
		List<Product> result = new ArrayList<Product>();
		List<Product> list = list(name);
		for (Product c : list) {
			if (!c.getProductId().equals(productId)) {
				result.add(c);
				break;
			}
		}
		return result;
	}
	
	/**
	 * 商品 
	 * @return
	 */
	public Object listProductId() {
		List<Product> productList = productDao.listAll();
		List<Long> result = new ArrayList<>();
		transferListId(productList, result);
		return result;
	}
	
	private void transferListId(List<Product> productList, List<Long> result) {
		for (Product item : productList) {
			result.add(item.getProductId());
		}
	}

	private void transferList(List<Product> productList, List<ListVo> result) {
		for (Product item : productList) {
			ListVo vo = new ListVo();
			vo.setKey(item.getProductId());
			vo.setLabel(item.getName());
			result.add(vo);
		}
		
	}

	/**
	 * 商品列表
	 * @param bo
	 * @return
	 */
	public Object list(SearchProductBo bo) {
		PageVo<ListProductVo> result = new PageVo<ListProductVo>();
        result.setPageNum(bo.getPageNum());
        result.setPageSize(bo.getPageSize());
        result.setItems(productDao.list(bo));
        result.setTotal(productDao.countList(bo));
		return result;
	}
	
	/**
	 * 商品详情
	 * @param productId
	 * @return
	 * @throws OceanException
	 */
	public Object detail(Long productId) throws OceanException {
		Object result = productDao.detail(productId);
		if (result == null) {
			throw new OceanException(ExceptionCode.数据不存在);
		}
		return result;
	}
	
	

}
